<?php
/**
 * Notes:消息模型
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/9/30
 * Time: 22:29
 */

namespace app\model;


use think\Model;

class Chat extends Model
{
    protected $table = 'chat';
    protected $pk = 'chat_id';
    public function del($chat_id){
        return self::get($chat_id)->delete();
    }
    public function edit($chat_id,$data){
        return self::allowField(true)->save($data,["chat_id" => $chat_id]);
    }

    /**
     * @param $user_id
     * @param $chat_target
     * @return Chat
     */
    public function getChatByUser($user_id,$chat_target){
        return self::where([
            'chat_target' => $chat_target,
            'user_id' => $user_id
        ])->select();
    }
    public function addChat($data){
        return self::create($data);
    }
    public function check($chat_id){
        return self::where(["chat_id"=>$chat_id])->count();
    }
    public function getById($chat_id){
        return self::get($chat_id);
    }


    /**
     * 消息类型转换
     * @param $value
     * @return mixed
     */
    public function getType($value){
        $type = [
            '1' => '好友',
            '2' => '群聊',
        ];
        return $type[$value];
    }

    /**
     * 消息发送方式
     * @param $value
     * @return mixed
     */
    public function getMod($value){
        $mod = [
            '1' => '消息',
            '2' => '文件',
            '3' => '审批'
        ];
        return $mod[$value];
    }
    public function getChatTimeAttr($value){
        return date('Y-m-d H:i:s', $value);
    }
}